package com.shyroke.daydayzhuanapi.mapper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.shyroke.daydayzhuanapi.dto.ProjectAddRequest;
import com.shyroke.daydayzhuanapi.dto.ProjectAppDto;
import com.shyroke.daydayzhuanapi.dto.ProjectDto;
import com.shyroke.daydayzhuanapi.dto.TypeDto;
import com.shyroke.daydayzhuanapi.entity.ProjectEntity;
import com.shyroke.daydayzhuanapi.entity.SubjectEntity;
import com.shyroke.daydayzhuanapi.entity.TypeEntity;
import com.shyroke.daydayzhuanapi.entity.UserEntity;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class ProjectMapper {
    @Autowired
    private ModelMapper modelMapper;

    public ProjectDto entityToDto(ProjectEntity e) {
        ProjectDto dto = modelMapper.map(e, ProjectDto.class);
        if(dto.getCreateTime()!=null){
            dto.setShowCreateTime(DateUtil.format(dto.getCreateTime(),"yyyy-MM-dd"));
        }
        TypeDto type = dto.getTypeObj();
        dto.setTypes(type.getTypeName());
        List<SubjectEntity> subjectList = e.getSubjectList();
        if(CollectionUtil.isNotEmpty(subjectList)){
            List<Integer> idList = subjectList.stream().map(s -> s.getId()).collect(Collectors.toList());
            dto.setSubjectIdList(idList);
        }
        dto.setDetailUrl("https://www.daydayzhuan.com/article/"+ dto.getId()+"?channel=APP");
        return dto;
    }

    public ProjectEntity dtoToEnTity(ProjectDto project) {
        ProjectEntity entity = modelMapper.map(project, ProjectEntity.class);
        entity.setTodayReadCount(0);
        entity.setTodayAppReadCount(0);
        return entity;
    }

    public ProjectEntity addRequestToEntity(ProjectAddRequest dto,ProjectEntity entity) {
        entity.setCommented(dto.getCommented());
        Integer typeId = Integer.valueOf(dto.getTypes());
        entity.setTypeId(typeId);
        entity.setContent(dto.getContent());
        entity.setCover(dto.getCover());
        entity.setIsHomePage(dto.getIsHomePage());
        entity.setIsNofollow(dto.getIsNofollow());
        entity.setKeyword(dto.getKeyword());
        entity.setOffline(dto.getOffline());
        entity.setSummary(dto.getSummary());
        entity.setTitle(dto.getTitle());
        entity.setType("A");
        entity.setUpdateTime(new Date());
        entity.setProfit(dto.getProfit());
        entity.setProjectName(dto.getProjectName());
        entity.setShowLabel(dto.getShowLabel());
        return entity;
    }

    public ProjectAppDto entityToAppDto(ProjectEntity e) {
        ProjectAppDto dto = modelMapper.map(e, ProjectAppDto.class);
        dto.setDetailUrl("https://www.daydayzhuan.com/article/"+ dto.getId()+"?channel=APP");
        return dto;
    }
}
